package com.zss.rocketmq.consumer.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("rocketmq-consumer")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zss.rocketmq"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(getGlobalParamList());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("rocketmq消费者 Swagger2接口文档") //文档首页标题
                .description("Swagger2接口文档") //文档描述信息
                .contact(new Contact("zhangsense", "url", "18239395309@163.com")) //创建者信息
                .termsOfServiceUrl("git@git.kaiban.git")
                .version("0.0.1") //文档版本
                .build();
    }

    /**
     * 获取全局的swagger来源的参数
     * @return
     */
    private List<Parameter> getGlobalParamList(){
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        parameterBuilder.name("swaggerSource").description("用于全局标识swagger来源的接口访问，业务接口不用理会该参数，请忽略").modelRef(new ModelRef("string")).parameterType("header").required(true).defaultValue("1").hidden(true).build();
        Parameter parameter = parameterBuilder.build();
        pars.add(parameter);
        return pars;
    }
}
